package com.zhoukun.cloud.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zhoukun.cloud.mapper.seataOrderMapper;
import com.zhoukun.cloud.CommentRest;
import com.zhoukun.cloud.pojo.tOrder;
import com.zhoukun.cloud.service.AccountService;
import com.zhoukun.cloud.service.StorageService;
import com.zhoukun.cloud.service.seataOrderService;
import io.seata.spring.annotation.GlobalTransactional;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import sun.rmi.log.LogInputStream;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

/**
 * @author zhoukun 86547462@qq.com
 * @version 1.0
 * @date 2020/11/2 21:40
 */
@Service
@Slf4j
public class seataOrderServiceImpl extends ServiceImpl<seataOrderMapper, tOrder> implements seataOrderService {

    @Autowired
    private seataOrderMapper orderMapper;

    @Resource
    private AccountService accountService;//用户微服务
    @Resource
    private StorageService storageService;//商品微服务


    //创建订单
    @GlobalTransactional(name = "createOrder",rollbackFor = Exception.class)
    public CommentRest create(tOrder order) {
        log.info("开始创建订单====》");
        orderMapper.insert(order);
        log.info("微服务调用商品库存，做减法");
       CommentRest storageComment=storageService.up(order.getProductId(),order.getCount());
        log.info("微服务调用个人账户，做减法");
        CommentRest accountComment=accountService.up(order.getUserId(),order.getMoney());


        if(storageComment.getCode()==200&&accountComment.getCode()==200)
        {
            return new CommentRest().setCode(200).setMessage("success");
        }else {
            return new CommentRest().setCode(400).setMessage("err").setData(storageComment.toString()+accountComment);
        }

    }



}
